package xyz.xiezc.spider.prase;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import xyz.xiezc.ioc.annotation.Component;
import xyz.xiezc.spider.entity.MzituBean;
import xyz.xiezc.spider.common.Request;
import xyz.xiezc.spider.common.Response;
import xyz.xiezc.spider.common.XBean;
import xyz.xiezc.spider.common.XUtil;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wb-xzc291800
 * @date 2019/04/18 17:48
 */
@Slf4j
@Component
public class ZipaiHtmlParseImpl implements ParseResponse {
    /**
     * 自拍图列表页面匹配 https://www.mzitu.com/zipai/comment-page-1/
     */
    Pattern zipaiPattern = Pattern.compile("https://www.mzitu.com/zipai/comment-page-(\\d+?)/");

    @Override
    public  XBean parse(XBean xBean)  {
        this.dealZipaiPhotoMenu(xBean);
        return xBean;
    }

    /**
     * 自拍图列表页
     *
     * @param xBean
     */
    private  void dealZipaiPhotoMenu(XBean xBean) {
        Request request = xBean.getRequest();
        String url = request.getUrl();

        Matcher matcher = zipaiPattern.matcher(url);
        //是自拍图列表页
        if (matcher.matches()) {
            Response response = xBean.getResponse();
            Document document = XUtil.getDocument(response);
            //查看是否设置了套图类
            Integer albumId = (Integer) request.getExtra().get("albumId");
            if (albumId == null) {
                MzituBean mzituBean = new MzituBean();
                mzituBean.setType("自拍");
                mzituBean.setTitle("妹子自拍");
                mzituBean.setPublishTime(null);
                mzituBean.setFetchUrl("https://www.mzitu.com/zipai/");
                xBean.addBean(mzituBean);
                request.addExtra("title", mzituBean.getTitle());
            }

            Elements imgDivs = document.select("#comments > ul > li > div");
            imgDivs.forEach(element -> {
                //图片链接
                Elements img = element.getElementsByTag("img");
                String src = img.attr("data-original");
                //图片时间
                String time = element.select("div.comment-meta > a").text();
                //构建图片链接
                Request request1 = new Request();
                request1.setUrl(src);
                request1.setMethod("get");
                request1.addExtra("showTime", time);
                request1.addExtra(request.getExtra());
                xBean.addNextRequest(request1);
            });

            //找到规则构建url
            String group1 = matcher.group(1);
            Integer currentPageNo = Integer.valueOf(group1);
            Integer zipaiMaxPageNo = (Integer) request.getExtra().get("zipaiMaxPageNo");
            if (zipaiMaxPageNo == null || currentPageNo > zipaiMaxPageNo) {
                //根据规则构建页面
                createZipaiUrls(xBean, document, group1);
            }
            xBean.setStatus(3);
        }
    }

    private void createZipaiUrls(XBean xBean, Document document, String group1) {
        Request request = xBean.getRequest();
        String url = request.getUrl();
        AtomicInteger maxPageNo = new AtomicInteger(Integer.valueOf(group1));
        //分页列表. 获得最大的页码
        Elements pageList = document.select("#comments > div > a");
        pageList.forEach(element -> {
            String href = element.attr("href");
            if (href.endsWith("#comments")) {
                href = href.substring(0, href.length() - 9);
            }

            Matcher matcher = zipaiPattern.matcher(href);
            if (matcher.matches()) {
                String group = matcher.group(1);
                Integer everyPageNo = Integer.valueOf(group);
                if (maxPageNo.get() < everyPageNo) {
                    maxPageNo.set(everyPageNo);
                }
            }
        });

        //找到前面相同的部分
        String prefixUrl;
        if (url.endsWith("/#comments")) {
            prefixUrl = url.substring(0, url.length() - 10 - group1.length());
        } else {
            prefixUrl = url.substring(0, url.length() - 1 - group1.length());
        }

        //组建url
        Integer zipaiMaxPageNo = maxPageNo.get();
        request.getExtra().put("zipaiMaxPageNo", zipaiMaxPageNo);
        for (int i = zipaiMaxPageNo; i > 0; i--) {
            Request request1 = new Request();
            request1.setUrl(prefixUrl + Objects.toString( i) + "/");
            request1.setMethod("get");
            request1.addExtra(request.getExtra());
            xBean.addNextRequest(request1);
        }
    }


}

